package course.ya.doc.from.code.model.lexer.project;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class XMLParser 
{
	private DocumentBuilderFactory factory;
	private  DocumentBuilder builder;
	private TransformerFactory transformerFactory;
	private String defautNameFile;			
	public XMLParser(String nameFile)throws ParserConfigurationException,
							TransformerException 	
	{
		
		factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
        transformerFactory = TransformerFactory.newInstance();     
        defautNameFile=nameFile;        
	}
	
	public boolean NewXMLKeyWords(String[] nameWords,int[] isGroup)
					throws TransformerException
			
	{
		if(nameWords.length==isGroup.length)
		{
			Document document=builder.newDocument();
			Element rootElement=document.createElement("KeyWords");
			document.appendChild(rootElement);
		
			for(int i=0;i<nameWords.length;i++)
			{
				Element words=document.createElement("Words");
				rootElement.appendChild(words);
		
				Attr atribut=document.createAttribute("name");
				atribut.setValue(nameWords[i]);
				words.setAttributeNode(atribut);
				
				Element name=document.createElement("name");
				name.appendChild(document.createTextNode(nameWords[i]));
				words.appendChild(name);
				
				Element groupOperator=document.createElement("groupOperator");
				groupOperator.appendChild(document.createTextNode(Integer.toString(isGroup[i])));
				words.appendChild(groupOperator);				
			}
			
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(document);
			StreamResult streamResult = new StreamResult(new File(defautNameFile));
			transformer.transform(domSource, streamResult);
			
			return true;	
			
		}
		return false;
	}
	
	public TreeMap<String,Integer> ReKeyWords(String nameFile) throws SAXException, IOException
	{
		TreeMap<String,Integer> words= new TreeMap<String, Integer>();
		
		File xmlFile = new File(nameFile);
		Document document = builder.parse(xmlFile);
		document.getDocumentElement().normalize();
		
		NodeList nodeList = document.getElementsByTagName
				(document.getDocumentElement().getChildNodes().
				item(1).getNodeName());
		for(int i=0;i<nodeList.getLength();i++)
		{
			Node node=nodeList.item(i);
			if(node.getNodeType() == Node.ELEMENT_NODE)
			{
				 Element element = (Element)node;
				
				words.put(
						element.getElementsByTagName("name").item(0).
						getChildNodes().item(0).getNodeValue(),
						Integer.parseInt(element.getElementsByTagName("groupOperator").
						item(0).getChildNodes().item(0).getNodeValue()));
				 
			}
				
		}
		return words;
		
				
	}


	public boolean WordConfigure(String nameFile,String keyWord,String replaceName,int isGroup) 
				 throws SAXException, IOException, TransformerException 
	{
		TreeMap<String,Integer> map=new TreeMap<String, Integer>();
		map=ReKeyWords(nameFile);
		map.remove(keyWord);
		map.put(replaceName, isGroup);
		Set<String> keys = map.keySet();
		Iterator<String> iterator = keys.iterator();
		String[] NewWords=new String[map.size()];
		int[] Mas=new int[map.size()];
	    int i=0;
		while (iterator.hasNext()) 
		{
		   NewWords[i] = (String)iterator.next();
		   Mas[i]=map.get(NewWords[i]);
		   i++;
		}
		NewXMLKeyWords(NewWords,Mas);		
		return true;
	}
	public String GetNameFile()
	{
		return defautNameFile;
	}
}

